package com.battle.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.TacticsType;
import com.battle.pojo.entity.User;
import com.battle.pojo.param.tacticsType.*;
import com.battle.pojo.vo.tacticsType.*;
import com.battle.pojo.dto.tacticsType.*;
import com.battle.service.repository.in.TacticsTypeRepository;
import com.battle.service.repository.in.UserRepository;
import com.battle.service.service.in.TacticsTypeService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 【描 述】：战术类型服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class TacticsTypeServiceImpl implements TacticsTypeService {

    private final TacticsTypeRepository tacticsTypeRepository;
    private final UserRepository userRepository;

    public TacticsTypeServiceImpl(TacticsTypeRepository tacticsTypeRepository, UserRepository userRepository) {
        this.tacticsTypeRepository = tacticsTypeRepository;
        this.userRepository = userRepository;
    }


    /**
     * 创建战术类型
     *
     * @param param
     */
    @Override
    public void createTacticsType(CreateTacticsTypeParam param) {
        TacticsType tacticsType = new TacticsType();
        BeanUtils.copyProperties(param, tacticsType);
        Long id = IdGenerator.SNOW.generate();
        tacticsType.setId(id);
        tacticsTypeRepository.create(tacticsType);
    }

    /**
     * 修改战术类型
     *
     * @param param
     */
    @Override
    public void editTacticsType(EditTacticsTypeParam param) {
        Long id = param.getId();
        TacticsType tacticsType = tacticsTypeRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, tacticsType);
        tacticsTypeRepository.edit(tacticsType);
    }

    /**
     * 移除战术类型
     *
     * @param id
     */
    @Override
    public void removeTacticsType(Long id) {
        tacticsTypeRepository.removeById(id);
    }

    /**
     * 启动/禁用战术类型
     *
     * @param id
     */
    @Override
    public void enabledTacticsType(Long id) {
        TacticsType tacticsType = tacticsTypeRepository.getByIdCheck(id);
        tacticsType.setIsDisable(!tacticsType.getIsDisable());
        tacticsTypeRepository.edit(tacticsType);
    }

    /**
     * 获取战术类型详情
     *
     * @param id
     * @return
     */
    @Override
    public GetTacticsTypeVo getTacticsType(Long id) {
        TacticsType tacticsType = tacticsTypeRepository.getByIdCheck(id);
        GetTacticsTypeVo vo = new GetTacticsTypeVo();
        BeanUtils.copyProperties(tacticsType, vo);
        return vo;
    }

    /**
     * 获取战术类型 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetTacticsTypeListVo> getTacticsTypeList(GetTacticsTypeListParam param) {

        SelectTacticsTypeDto dto = new SelectTacticsTypeDto();
        BeanUtils.copyProperties(param, dto);
        Page<TacticsType> page = tacticsTypeRepository.selectTacticsType(dto);
        List<TacticsType> tacticsTypeList = page.getRecords();
        List<GetTacticsTypeListVo> list = new ArrayList<>();

        //创建人
        if (CollectionUtil.isNotEmpty(tacticsTypeList)) {
            List<Long> userIds = new ArrayList<>();
            tacticsTypeList.forEach(v -> {
                userIds.add(v.getCreator());
            });

            Map<Long, User> userMap = userRepository.getMap(userIds);
            for (TacticsType tacticsType : tacticsTypeList) {
                GetTacticsTypeListVo vo = new GetTacticsTypeListVo();
                BeanUtils.copyProperties(tacticsType, vo);

                User user = userMap.get(tacticsType.getCreator());
                if (user!=null){
                    vo.setCreateName(user.getName());
                }
                list.add(vo);
            }
        }

        return new PageResult<>(list, page);

    }

    /**
     * 获取战术类型下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetTacticsTypeDownListVo> getTacticsTypeDownList(GetTacticsTypeDownListParam param) {
        TacticsType po = new TacticsType();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<TacticsType> tacticsTypeList = tacticsTypeRepository.getListByEntity(po);

        List<GetTacticsTypeDownListVo> list = new ArrayList<>();
        for (TacticsType tacticsType : tacticsTypeList) {
            GetTacticsTypeDownListVo vo = new GetTacticsTypeDownListVo();
            BeanUtils.copyProperties(tacticsType, vo);
            list.add(vo);
        }
        return list;
    }


}



